<?php
/**
 * Created by PhpStorm.
 * User: sin
 * Date: 2019/3/18
 * Time: 1:47 PM
 */

namespace app\partner\controller;

use app\common\cache\user\IUserCache;
use app\common\controller\PartnerBaseController;
use app\common\enum\IsMarry;
use app\common\enum\IsOverdue;
use app\common\enum\IsReal;
use app\common\enum\IsYes;
use app\common\ext\IDb;
use app\common\ext\IIdentity;

class Asset extends PartnerBaseController{

    /**
     * @功能：设置用户身份信息
     * @开发者：cxl
     */
    public function info_real(){
        //接收参数
        $real = self::get_param('real',true,self::NOT_EMPTY,'姓名');
        $identity = self::get_param('identity',true,self::IDENTITY,'身份证号');
        $identity_front_url = self::get_data('identity_front_url');
        $identity_back_url = self::get_data('identity_back_url');
//        $sex = self::get_data('sex');
//        $age = self::get_data('age');
        $birth_address = self::get_data('birth_address');

        $user_info = IDb::getInstance('user')->setDbWhere(['identity'=>$identity,'user_type'=>2])->row();
//        if($user_info){
//            if($user_info['user_type'] != 2){
//                self::set_code(self::WARNING_0001);
//                self::set_msg("该身份证号注册过，无法录入！");
//                self::send();
//            }
//
//            if($user_info['is_real'] != IsReal::yes){
//                self::set_code(self::WARNING_0002);
//                self::set_msg("已实名认证过，无法修改！");
//                self::send();
//            }
//        }

        //设置用户名称及身份证号码
        $user_data['real'] = $real;
        $user_data['identity'] = $identity;

        $identity_info = IIdentity::getInfo($identity);
        if(is_array($identity_info)){
            $user_data['sex'] = $identity_info['sex'];
            $user_data['age'] = $identity_info['age'];
        }

        // 如未上传身份证正反面和户籍，则可修改
        /*if(empty($user_info['birth_address']) && !empty($birth_address)) */$user_data['birth_address'] = $birth_address;
        if(empty($user_info['identity_front_url']) && !empty($identity_front_url)) $user_data['identity_front_url'] = $identity_front_url;
        if(empty($user_info['identity_back_url']) && !empty($identity_back_url)) $user_data['identity_back_url'] = $identity_back_url;

        //判断是否有需要修改信息
        if(empty($user_data)){
            //操作成功
            self::set_code(self::SUCCESS);
            self::set_msg("成功！");
            self::set_values($user_info);
            self::send();
        }

        if(!$user_info){
            $user_data['user_type'] = 2;
            $user_data['is_real'] = IsReal::not;
            $user_data['cooperate_id'] = self::$info['cooperate_id'];
            $user_data['cooperate_admin_id'] = self::$uid;
            $user_id = IDb::getInstance("user")->setDbData($user_data)->add();
            if($user_id === false){
                self::set_code(self::WARNING_0002);
                self::set_msg("添加客户信息失败！");
                self::send();
            }
        }else{
            $user_id = $user_info['user_id'];

            //检查用户所属合作商
            $result = self::check_user_belong($user_id);
            if($result !== true){
                self::set_code(self::WARNING_0001);
                self::set_msg($result);
                self::send();
            }

            //设置用户昵称
            $user_where['user_id'] = $user_id;
            $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
            if($user_upd === false){
                self::set_code(self::WARNING_0004);
                self::set_msg("修改用户基础信息失败！");
                self::send();
            }

            //清除缓存
            IUserCache::deleteInfo($user_id);
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value('user_id',$user_id);
        self::send();
    }
    /**
     * @功能：设置用户信息
     * @开发者：gys
     */
    public function info_user(){
        //接收参数
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');
        $is_marry = self::get_data('is_marry',self::INT,'请选择婚姻状况');
        $couple_name = self::get_data('couple_name');
        $couple_phone = self::get_data('couple_phone');
        $education = self::get_data('education',self::INT,'请选择文化程度');
        $loan_phone = self::get_data('loan_phone',self::NOT_EMPTY,'手机号码不能为空');
        self::check_data($loan_phone,self::PHONE,'手机号码',true);
        $domicile_address = self::get_data('domicile_address');
        $occupation = self::get_data('occupation',self::INT,'请选择职业身份');
        $monthly_income = self::get_data('monthly_income',self::NOT_EMPTY,'月均收入不能为空');

        if($couple_phone != ''){
            self::check_data($couple_phone,self::PHONE,'配偶手机号码',true);
        }

        $user_info = IDb::getInstance('user')
            ->setDbFiled('user_id,cooperate_admin_id')
            ->setDbWhere(['phone'=>$loan_phone,'user_type'=>2])
            ->row();
        if($user_info && ($user_info['cooperate_admin_id'] != self::$uid || ($user_info['cooperate_admin_id'] == self::$uid && $user_info ['user_id'] != $user_id))){
            self::set_code(self::WARNING_0001);
            self::set_msg('该手机号码已录入');
            self::send();
        }

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        if($is_marry == IsMarry::married){
            //已婚
            $user_data['couple_name'] = $couple_name;
            $user_data['couple_phone'] = $couple_phone;
        }else if($is_marry == IsMarry::divorce){
            //离异
            $user_data['couple_name'] = ['exp','null'];
            $user_data['couple_phone'] = ['exp','null'];
        }else{
            //未婚
            $is_marry = IsMarry::unmarried;
            $user_data['couple_name'] = ['exp','null'];
            $user_data['couple_phone'] = ['exp','null'];
        }

        //设置用户信息
        $user_data['is_marry'] = $is_marry;
        $user_data['education'] = $education;
        $user_data['nick'] = get_string_rsa($loan_phone, 3, 4) . "_" . rand(1000,9999);
        $user_data['phone'] = $loan_phone;
        $user_data['loan_phone'] = $loan_phone;
        $user_data['domicile_address'] = $domicile_address;
        $user_data['occupation'] = $occupation;
        $user_data['monthly_income'] = $monthly_income;

        $user_upd = IDb::getInstance("user")->setDbWhere(['user_id'=>$user_id])->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("添加客户信息失败！");
            self::send();
        }

        //清除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置用户资产基本信息
     * @开发者：gys
     */
    public function info_asset(){
        //接收参数
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');
        $is_company = self::get_data('is_company',self::NOT_EMPTY,'是否有公司不能为空');
        $is_house = self::get_data('is_house',self::NOT_EMPTY,'是否有房不能为空');
        $is_credit_card = self::get_data('is_credit_card',self::NOT_EMPTY,'是否有信用卡不能为空');
        $is_social = self::get_data('is_social',self::NOT_EMPTY,'是否有社保不能为空');
        $is_accumulation_fund = self::get_data('is_accumulation_fund',self::NOT_EMPTY,'是否有公积金不能为空');
        $is_overdue = self::get_data('is_overdue',self::NOT_EMPTY,'是否有逾期不能为空');
//        $is_white = self::get_data('is_white');
        $is_court = self::get_data('is_court',self::NOT_EMPTY,'是否有法院执行记录不能为空');
        $is_policy = self::get_data('is_policy',self::NOT_EMPTY,'是否有保单不能为空');
        $is_car = self::get_data('is_car',self::NOT_EMPTY,'是否有车不能为空');
        $is_loan = self::get_data('is_loan',self::NOT_EMPTY,'是否有贷款不能为空');

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        //设置用户信息
        $user_where['user_id'] = $user_id;
        $user_data['is_company'] = $is_company;
        $user_data['is_house'] = $is_house;
        $user_data['is_credit_card'] = $is_credit_card;
        $user_data['is_social'] = $is_social;
        $user_data['is_accumulation_fund'] = $is_accumulation_fund;
//        $user_data['is_white'] = $is_white;
        $user_data['is_overdue'] = $is_overdue;
        $user_data['is_court'] = $is_court;
        $user_data['is_policy'] = $is_policy;
        $user_data['is_car'] = $is_car;
        $user_data['is_loan'] = $is_loan;

        IDb::dbStartTrans();

        //同步个人信息
        $result = \app\common\ext\User::syncUserInfoToMaterial($user_id,$user_data);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0001);
            self::set_msg(\app\common\ext\User::getError());
            self::send();
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置逾期信息
     * @开发者：cxl
     */
    public function info_overdue(){
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        $data['half_year'] = [];
        $data['one_year'] = [];
        $data['three_year'] = [];
        $data['continuity'] = [];

        $data['half_year']['is_overdue'] = self::get_data('half_year',self::NOT_EMPTY,"请选择最近半年是否逾期！");
        $data['one_year']['is_overdue'] = self::get_data('one_year',self::NOT_EMPTY,"请选择最近一年是否逾期！");
        $data['three_year']['is_overdue'] = self::get_data('three_year',self::NOT_EMPTY,"请选择最近三年是否逾期");
        $data['continuity']['is_overdue'] = self::get_data('continuity',self::NOT_EMPTY,"请选择是否连续逾期");
        
        $data['half_year']['is_overdue'] = $data['half_year']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;
        $data['one_year']['is_overdue'] = $data['one_year']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;
        $data['three_year']['is_overdue'] = $data['three_year']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;
        $data['continuity']['is_overdue'] = $data['continuity']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;

        $data['half_year']['less_30'] = intval(self::get_data('half_year_less_30'));
        $data['half_year']['over_30'] = intval(self::get_data('half_year_over_30'));
        $data['half_year']['over_60'] = intval(self::get_data('half_year_over_60'));
        $data['half_year']['over_90'] = intval(self::get_data('half_year_over_90'));

        $data['one_year']['less_30'] = intval(self::get_data('one_year_less_30'));
        $data['one_year']['over_30'] = intval(self::get_data('one_year_over_30'));
        $data['one_year']['over_60'] = intval(self::get_data('one_year_over_60'));
        $data['one_year']['over_90'] = intval(self::get_data('one_year_over_90'));

        $data['three_year']['less_30'] = intval(self::get_data('three_year_less_30'));
        $data['three_year']['over_30'] = intval(self::get_data('three_year_over_30'));
        $data['three_year']['over_60'] = intval(self::get_data('three_year_over_60'));
        $data['three_year']['over_90'] = intval(self::get_data('three_year_over_90'));

        $data['continuity']['less_30'] = intval(self::get_data('continuity_less_30'));
        $data['continuity']['over_30'] = intval(self::get_data('continuity_over_30'));
        $data['continuity']['over_60'] = intval(self::get_data('continuity_over_60'));
        $data['continuity']['over_90'] = intval(self::get_data('continuity_over_90'));

        $user_where['user_id'] = $user_id;
        $user_data['overdue_info'] = json_encode($data);

        IDb::dbStartTrans();
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("修改用户基础信息失败！");
            self::send();
        }

        //同步个人信息
        $result = \app\common\ext\User::syncMaterialToUserInfo($user_id,\app\common\ext\User::MATERIAL_OVERDUE);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0003);
            self::set_msg("同步逾期信息失败！");
            self::send();
        }

        IDb::dbCommit();

        //清除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：获取逾期信息
     * @开发者：cxl
     */
    public function overdue_info(){
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        $overdue_info = IUserCache::getInfo($user_id,'overdue_info');
        if(empty($overdue_info)){
            $overdue_info = [];
        }else{
            $overdue_info = json_decode($overdue_info);
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_value('overdue_info',$overdue_info);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：获取用户信息
     * @开发者：gys
     */
    public function user_info(){
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        $user_info = IUserCache::getInfoWithoutSensitive($user_id);
        if(empty($user_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("客户不存在！");
            self::send();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：获取用户资产信息详情
     * @开发者：gys
     */
    public function asset_info(){
        $user_id = self::get_data('user_id',self::NOT_EMPTY,'客户ID不能为空');

        //检查用户所属合作商
        $result = self::check_user_belong($user_id);
        if($result !== true){
            self::set_code(self::WARNING_0001);
            self::set_msg($result);
            self::send();
        }

        $data = \app\common\ext\User::getAsset($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($data);
        self::send();
    }
}